import AchieveApi from '../../api/achieve'
import CategoryApi from '../../api/category'
import achieve from "../../api/achieve";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperImageList: [
      {
        id: 1,
        src: '../../static/profile/guet-1.jpg',
      },
      {
        id: 2,
        src: '../../static/profile/guet-2.jpg',
      },
      {
        id: 3,
        src: '../../static/profile/guet-3.jpg',
      }
    ],
    categoryList: [], // 当前所查询的成果类型
    tabActive: 0, // 当前tab激活的下标
    none: true, // 是否没有数据
    achieveList: [], // 成果列表
    loading: false, // 是否显示正在加载字样
    loaded: false, // 是否显示已加载完成字样
    index: 0, // 当前查询到的成果类型的下标 categoryList下标
    showFilter: false, // 是否展示筛选弹框
    params: {
      page: 1,
      size: 4,
      key: '',
      indexName: '',
      status: null,
      number: wx.getStorageSync('user').number,
      // array: ['美国', '中国', '巴西', '日本'],
    }, // 搜索和筛选的参数
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.showLoading({
      title: '加载中',
    })
    // 查看所有的成果类型
    var queryInfo = {
      pagination: false
    }
    let that = this
    CategoryApi.getCategoryList(queryInfo).then(res => {
      that.setData({
        categoryList: res.data.data
      })
      that.selectOwnAchieveList()
      wx.hideLoading()
    })
  },
  onCloseFilter() {
    this.setData({
      showFilter: false
    })
  },
  chooseAchieve() {
    // this.setData({
    //   showFilter: true
    // })
    wx.navigateTo({
      url: '/pages/index/choose/index',
    })
  },
  // 根据筛选的结果继续搜索成果
  searchAchieve(e) {
    this.setData({
      ['params.key']: e.detail,
      ['params.status']: null,
      ['params.page']: 1,
      achieveList: [],
      index: 0,
      loaded: false
    })
    this.selectOwnAchieveList()
  },
  selectOwnAchieveList(){
    wx.showLoading({
      title: '加载中...',
    })

    this.setData({
      ['params.number']: wx.getStorageSync("user").number
    })
    var that = this
    AchieveApi.getOwnAchieveList(this.data.params).then(res => {
      if (res.data.code === 200) {
        that.setData({
          achieveList: that.data.achieveList.concat(res.data.data.records)
        })
        if (res.data.data.pages === that.data.params.page) {
          that.setData({
            loaded: true
          })
        }
      } else {
        wx.showToast({
          title: res.data.data,
          icon: 'none'
        })
      }
      wx.hideLoading()
    })
  },
  clickItem({currentTarget}){
    wx.navigateTo({
      url: '/pages/achieve/index',
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptItem', {
          data: currentTarget.dataset.item
        })
      }
    })
  },
  deleteAchieve({target:{dataset:{item}}}) {
    var that = this
    wx.showLoading({
      title: '加载中...'
    })
    AchieveApi.deleteAchieve(item.id).then(res => {
      if (res.data.code === 200) {
        this.setData({
          ['params.page']: 1,
          achieveList: [],
        })
        wx.showToast({
          title: '删除成功!',
          icon: 'success'
        })
        that.selectOwnAchieveList()
      } else {
        wx.showToast({
          title: res.data.data,
          icon: 'none'
        })
      }
    })
    wx.hideLoading()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (!this.data.loaded) {
      this.setData({
        loading: true,
        ['params.page']: this.data.params.page + 1
      })
      this.selectOwnAchieveList()
      this.setData({
        loading: false
      })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.showNavigationBarLoading()
    this.setData({
      loading: false,
      loaded: false,
      ['params.page']: 1,
      ['params.indexName']: '',
      ['params.key']: '',
      ['params.status']: null,
      achieveList: [],
      index: 0
    })
    this.selectOwnAchieveList()
    setTimeout(function () {
      wx.showToast({
        title: '刷新成功',
        icon: 'success',
        duration: 1000
      })
      wx.hideNavigationBarLoading()
      wx.stopPullDownRefresh()
    }, 1000)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },






})
